import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class VideoDemoPage extends StatefulWidget {
  const VideoDemoPage({super.key});

  @override
  State<VideoDemoPage> createState() => _VideoDemoPageState();
}

class _VideoDemoPageState extends State<VideoDemoPage> {
  // UI控制栏（进度条、播放、暂停按钮等，Chewiee独特的）
  late VideoPlayerController videoPlayerController;

  // 视频控制器（视频播放的核心，与video_player基本一致）
  late ChewieController chewieController;

  // 集成UI控制栏和视频播放器的组件
  late Chewie playerWidget;

  @override
  void initState() {
    super.initState();

    super.initState();
    // 视频控制器，播放网络视频，播放方式可以是[networkUrl, asset, file, contentUri]
    videoPlayerController = VideoPlayerController.networkUrl(
        Uri.parse('http://vjs.zencdn.net/v/oceans.mp4'))
      ..initialize().then((_) {
        // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
        setState(() {});
      });
    // 初始化视频控制器
    videoPlayerController.initialize();

    // 初始化控制栏
    chewieController = ChewieController(
      // 控制栏UI集成视频控制器
      videoPlayerController: videoPlayerController,
      autoPlay: false, // 自动播放
      looping: false, // 循环播放
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
      color: Colors.black,
      height: 300,
      child: AspectRatio(
        aspectRatio: videoPlayerController.value.aspectRatio,
        child: Chewie(
          controller: chewieController,
        ),
      ),
    ));
  }

  @override
  void dispose() {
    videoPlayerController.dispose();
    chewieController.dispose();
    super.dispose();
  }
}
